﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TaskSchedulingSystemForm.TaskMonitor;
using System.IO;
using TaskSchedulingSystemForm.Model;
using Common;
using TaskSchedulingSystemForm.Common;

namespace TaskSchedulingSystemForm
{
    public partial class CreateNewTask : Form
    {
        private static TaskConfig xmlConfig;
        public  delegate void FreshenOP(int opCommand);
        public FreshenOP FreshenTree;
        public CreateNewTask(TaskConfig configXml)
        {
            xmlConfig = configXml;
            InitializeComponent();
            this.setCronControl1.ReturnCronExpression = GetValidCron;
        }
        /// <summary>
        /// 界面加载时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateNewTask_Load(object sender, EventArgs e)
        {
            InitializeradioControl();
        }
        /// <summary>
        /// 初始化控件数据
        /// </summary>
        private void InitializeradioControl()
        {
            radioButton_exitType.Checked = true;
            textBox_newType.Enabled = false;
            foreach (var type in xmlConfig.allTaskType)
            {
                comboBox_Tasktype.Items.Add(type.TypeName);
            }
        }
        /// <summary>
        /// 获取有效的cron表达式
        /// </summary>
        /// <param name="cron"></param>
        private void GetValidCron(string cron)
        {
            string filePath = "TaskConfig.xml";
            TaskType taskType = new TaskType();
            taskType.taskLst = new List<TaskModel>();
            TaskModel task = new TaskModel();
            task.cronExpression = cron;
            //
            if (CheckPZDataIsRight())
            {
                task.taskClass = textBox_className.Text.ToString();
                task.taskName = textBox_taskName.Text.ToString();
                task.taskDllPath = textBox_taskdll.Text.ToString();
                if (radioButton_TypeNew.Checked)
                {
                    taskType.TypeName = textBox_newType.Text.ToString();
                    taskType.taskLst.Add(task);
                    xmlConfig.allTaskType.Add(taskType);
                    LocalCacheFactory.ToFile<TaskConfig>(filePath, xmlConfig);
                    FreshenTree(1);
                }
                else
                {
                    taskType.TypeName = comboBox_Tasktype.SelectedItem.ToString();
                    foreach(var ty in xmlConfig.allTaskType)
                    {
                        if(ty.TypeName == taskType.TypeName)
                        {
                            ty.taskLst.Add(task);
                            LocalCacheFactory.ToFile<TaskConfig>(filePath, xmlConfig);
                            FreshenTree(1);
                        }
                    }
                }
                DialogResult dr = MessageBox.Show("任务创建或修改成功！是否继续？","提示信息",MessageBoxButtons.OKCancel);
                if (dr == DialogResult.Cancel)
                {
                    this.Close();
                }
                else
                {
                    this.textBox_className.Clear();
                    this.textBox_newType.Clear();
                    this.textBox_taskdll.Clear();
                    this.textBox_taskName.Clear();
                }
                TSSLog.WrriteXTLog(task, TSSOP.CreateNewTask);
            }
        }
        /// <summary>
        /// radio按钮改变时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_exitType.Checked)
            {
                comboBox_Tasktype.Enabled = true;
                textBox_newType.Enabled = false;
            }
            else
            {
                comboBox_Tasktype.Enabled = false;
                textBox_newType.Enabled = true;
            }
        }
        /// <summary>
        /// 选择dll文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_finddll_Click(object sender, EventArgs e)
        {       
            openFile.Filter = "dll文件(*.dll)|*.dll";
            string basePath = AppDomain.CurrentDomain.BaseDirectory;
            string tmpPath = Path.Combine(basePath, "taskdll");
            openFile.InitialDirectory = tmpPath;
            DialogResult dr = openFile.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                string fileName = openFile.SafeFileName;
                this.textBox_taskdll.Text = openFile.FileName;
            }
        }
        /// <summary>
        /// 检查数据是否正确
        /// </summary>
        private bool CheckPZDataIsRight()
        {
            if (comboBox_Tasktype.Enabled)
            {
                if (textBox_className.Text == "")
                {
                    MessageBox.Show("任务类名不能为空！");
                    return false;
                }
            }
            if (textBox_newType.Enabled)
            {
                if (this.textBox_newType.Text == "" || comboBox_Tasktype.SelectedText == "请选择任务类型")
                {
                    MessageBox.Show("任务类型名不能为空！");
                    return false;
                }
            }
            if (textBox_taskName.Text == "")
            {
                MessageBox.Show("任务名不能为空！");
                return false;
            }
            if (textBox_taskdll.Text == "")
            {
                MessageBox.Show("任务程序集不能为空！");
                return false;
            }
            return true;
        }
      
    }
}
